/*
 * Copyright (C) 2010 Graham Allan
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.strath.cis.grallan.findbugs.adaptiveranking.population;

import static java.lang.String.format;
import edu.strath.cis.grallan.findbugs.adaptiveranking.UserDesignationToPositivesMap;
import edu.umd.cs.findbugs.BugInstance;

/**
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class AlertTypePopulation extends AbstractPopulation {

	private final String type;

	public AlertTypePopulation(UserDesignationToPositivesMap designationMapper, String type) {
		super(designationMapper);
		this.type = type;
	}

	public static AlertTypePopulation populationOf(String type) {
		return new AlertTypePopulation(new UserDesignationToPositivesMap(), type);
	}

	@Override
	protected void checkAlert(BugInstance bugAlert) throws PopulationException {
		super.checkAlert(bugAlert);
		String thatType = bugAlert.getType();
		if (thatType == null || "".equals(thatType)) {
			throw new PopulationException("The type of alert cannot be empty.");
		}
		if (!type.equalsIgnoreCase(thatType)) {
			String msg = format("This population is for alerts of type %s. The given bug alert has type %s", type,
					thatType);
			throw new PopulationException(msg);
		}
	}

	public String populationDescriptor() {
		return type;
	}
}
